<?php
include_once ('class.mysql.php');

class RegisterMachine
{
    private $comtec_id_mainteiner;
    private $comtec_id_owner;
    private $comtec_id_configman;
    private $tag_id_machine;
    private $cod_mainteiner;
    private $cod_owner;
    private $brand;
    private $model;
    private $year;
    private $notes;
    private $comtec_id_responsible;
    private $date_installation;
    private $status_reg;
    
    private $errors;
    private $token;

    public function __construct()
    {
        $this->errors   = array();
	
	$this->comtec_id_mainteiner	= $_POST['comtec_id_mainteiner'];
	$this->comtec_id_owner		= $_POST['comtec_id_owner'];
	$this->comtec_id_configman	= $_POST['comtec_id_configman'];
	$this->tag_id_machine		= $_POST['tag_id_machine'];
	$this->cod_mainteiner		= $_POST['cod_mainteiner'];
	$this->cod_owner		= $_POST['cod_owner'];
	$this->brand			= $_POST['brand'];
	$this->model			= $_POST['model'];
	$this->year			= $_POST['year'];
	$this->notes			= $_POST['notes'];
        if(strlen($this->notes) > 400)
        {
         $this->notes = substr($this->notes,0,400);   
        }
        $this->comtec_id_responsible    = $_POST['comtec_id_responsible'];
        $this->date_installation        = $_POST['date_installation'];
        
	$this->status_reg		= 1;
        $this->token			= $_POST['token'];
    }

    public function process()
    {
        if($this->valid_token() && $this->valid_data())
            $this->register();

        return count($this->errors)? 0 : 1;
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
    }

    public function filter_num($var)
    {
        return preg_replace('/[^0-9+]/','',$var);
    }

    public function register()
    {
        $db = new MySQL();
      
        $stmt = $db->query("SELECT ADDDATE('$this->date_installation',INTERVAL maintainment_period DAY) as date_next_maintenance FROM configman WHERE comtec_id_configman=$this->comtec_id_configman");            
        $row = mysql_fetch_array($stmt,MYSQL_NUM);  
        $date_next_maintenance = $row[0];
            
        $stmt = $db->query("INSERT INTO machines (
	    comtec_id_mainteiner,
	    comtec_id_owner,
	    comtec_id_configman,
	    tag_id_machine,
	    cod_mainteiner,
	    cod_owner,
	    brand,
	    model,
	    year,
	    notes,
	    status_reg,
            date_installation,
            date_next_maintenance,
            comtec_id_responsible
	) VALUES (
            $this->comtec_id_mainteiner,
            $this->comtec_id_owner,
	    $this->comtec_id_configman,
	    '$this->tag_id_machine',
            '$this->cod_mainteiner',
            '$this->cod_owner',
            '$this->brand',
            '$this->model',
            '$this->year',
            '$this->notes',
             $this->status_reg,
            '$this->date_installation',
            '$date_next_maintenance',
            $this->comtec_id_responsible
        )");

        if($db->affected_rows() < 1)
            $this->errors[] = 'No se puede procesar la inscripci&oacute;n';
    }

    public function user_exists()
    {
     $db = new MySQL();
     $stmt = $db->query("SELECT comtec_id_machine FROM machines WHERE tag_id_machine = '$this->tag_id_machine'");
     return $db->num_rows($stmt)? 1 : 0;
    }

    public function show_errors()
    {
        $msg = "";
        foreach($this->errors as $key=>$value)
            $msg = $msg . $value . "\n";
        return $msg;
    }

    public function valid_data()
    {
        if($this->user_exists())
            $this->errors[] = 'M&aacute;quina ya existe';

        return count($this->errors)? 0 : 1;
    }

    public function valid_token()
    {
        if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
            $this->errors[] = 'Registro inv&aacute;lido';

        return count($this->errors)? 0 : 1;
    }
}
?>
